package view.hall
{
	import control.hall.RoomListControl;
	import control.panel.IPanelControl;
	
	import events.TabEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.Event;
	
	import utils.DisplayTool;
	
	import view.components.SimpleLoading;
	import view.components.UIComponents;
	import view.components.tab.Tab;
	import view.components.tab.TabBar;
	import view.panel.AbsPanel;

	public class RoomsList extends AbsPanel
	{
		private var dataGrid:*;
		private var columns :Array;
		private var tab:Tab;
		private var simpleLoading:SimpleLoading;
		
		public function RoomsList(panelControl:IPanelControl)
		{
			super(panelControl);
			
			resetBg(580,275,1,false,0x333333,0);
			initTab();
			initLoading();
			initDataGrid();
			
			drawLine();
			dropShadow();
		}
		
		
		
		private function initLoading():void
		{
			simpleLoading = new SimpleLoading();
			
		}
		
		public function showLoading():void
		{
			simpleLoading.x = (this.width - simpleLoading.width) * .5 + simpleLoading.width * .5;
			simpleLoading.y = (this.height - simpleLoading.height) * .5 + simpleLoading.height* .5;
			simpleLoading.start();
			addChild(simpleLoading);
			this.dataGrid.graphics.clear();
		}
		
		public function hideLoading():void
		{
			simpleLoading.stop();
			DisplayTool.removeChild(this,simpleLoading);
			drawLine();
		}
		
		private function initTab():void
		{
			tab = new Tab();
			tab.addTabBar(new TabBar(0,"新手场",120),true);
			tab.addTabBar(new TabBar(1,"初级场",120));
			tab.addTabBar(new TabBar(2,"中级场",120));
			tab.addTabBar(new TabBar(3,"高级场",120));
			addChild(tab);
			tab.y = - 29;
			//tab.x = -1;
			tab.addEventListener(TabEvent.TABBAR_CLICK,onTabBarClick);
		}
		
		private function onTabBarClick(event:TabEvent):void
		{
			(panelControl as RoomListControl).onTabSwitch(tab.selectedIndex);
		}
		
		private function initDataGrid():void
		{
			columns  = ["id","name","betTimes","count","watchCount","deadLine","fee"];
			
			dataGrid = UIComponents.getDataGrid();
			dataGrid.centers = [false,false,true,true,true,false,true];
			dataGrid.setSize(570,265);
			dataGrid.columns = columns;
			addChild(dataGrid as DisplayObject);
			dataGrid.x = 5;
			dataGrid.y = 5;
			dataGrid.getColumnAt(0).headerText = "ID";
			dataGrid.getColumnAt(0).width = 50;
			dataGrid.getColumnAt(1).headerText = "房间";
			dataGrid.getColumnAt(1).width = 150;
			dataGrid.getColumnAt(2).headerText = "底价";
			dataGrid.getColumnAt(2).width = 60;
			dataGrid.getColumnAt(3).headerText = "玩家";
			dataGrid.getColumnAt(3).width = 60;
			dataGrid.getColumnAt(4).headerText = "观看";
			dataGrid.getColumnAt(4).width = 60;
			dataGrid.getColumnAt(5).headerText = "准入条件";
			dataGrid.getColumnAt(5).width = 80;
			dataGrid.getColumnAt(6).headerText = "座位收费";
			//dataGrid.getColumnAt(5).width = 100;
			
			dataGrid.setRendererStyle("skin",new Shape());
			dataGrid.resizableColumns = false;
			dataGrid.addEventListener("itemDoubleClick",onDataGridDClick);
			dataGrid.addEventListener("itemClick",onDataGridClick);
		}
		
		private function drawLine():void
		{
			with(this.dataGrid.graphics)
			{
				
				var lastWidth:int=0;
				var height:int = dataGrid.headerHeight;
				for (var i:int=0;i<dataGrid.getColumnCount() - 1; i++)
				{
					lastWidth += dataGrid.getColumnAt(i).width;
					lineStyle(1,0x000000);
					moveTo(lastWidth - 1,height);
					lineTo(lastWidth - 1,dataGrid.height);
					
					
					lineStyle(1,0x666666);
					moveTo(lastWidth,height);
					lineTo(lastWidth,dataGrid.height);
				}
			}	
		}
		
		public function get selectedItem():Object
		{
			return dataGrid.selectedItem;
		}
		
		private function onDataGridDClick(event:Event):void
		{
			(panelControl as RoomListControl).onItemDClick();
		}
		
		private function onDataGridClick(event:*):void
		{
			
			(panelControl as RoomListControl).onItemClick(event.item);
		}
		
		public function updateList(dataProvider:*):void
		{
			dataGrid.dataProvider = dataProvider;
			dataGrid.sortItemsOn("id",Array.NUMERIC);
		}
		
		public function clearList():void
		{
			dataGrid.removeAll();
		}
		
	}
}